//
//  RMImageBrowser.m
//  RebateMall
//
//  Created by BaizYi on 2018/5/7.
//  Copyright © 2018年 hehaoxian. All rights reserved.
//

#import "RMImageBrowser.h"
#import <Photos/Photos.h>
#import "YJYBannerView.h"

static UIImageView *orginImageView;
static CGRect _oldframe;
@interface RMImageBrowser ()<YJYBannerViewDelegate>

@property(assign,nonatomic) CGFloat screenWidth;
@property(assign,nonatomic) CGFloat screenHeight;




//@property(strong,nonatomic) UIScrollView  *scrollView;
//@property(strong,nonatomic) UIPageControl  *pageControl;
//@property(strong,nonatomic) UIImageView  *leftImageView;
//@property(strong,nonatomic) UIImageView  *rightImageView;
//@property(strong,nonatomic) UIImageView  *middleImageView;
//@property(assign,nonatomic) NSInteger currentIndex;


@end

@implementation RMImageBrowser

+(void)showImage:(UIImageView *)avatarImageView{
    RMImageBrowser *browser = [[RMImageBrowser alloc]initWithFrame:CGRectMake(0, 0, 0 , 0) avatarImage:avatarImageView];
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window.rootViewController.view addSubview:browser];
}



-(instancetype)initWithFrame:(CGRect)frame avatarImage:(UIImageView *)avatarImageView{
    if (self = [super initWithFrame:frame]) {
        self.screenWidth = [UIScreen mainScreen].bounds.size.width;
        self.screenHeight = [UIScreen mainScreen].bounds.size.height;
        self.frame = CGRectMake(0, 0, self.screenWidth, self.screenHeight);
        UIImage *image = avatarImageView.image;
        orginImageView = avatarImageView;

        //蒙板
        UIView *bgView = [[UIView alloc]initWithFrame:self.bounds];
        //将avatarImageView的坐标系转移到window上
        CGRect oldFrame = [avatarImageView convertRect:avatarImageView.bounds toView:self];
        bgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        bgView.alpha = 0.1;
        
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:oldFrame];
        imageView.image = image;
        imageView.tag = 1;
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = YES;
        [bgView addSubview:imageView];
//        [window addSubview:bgView];
        [self addSubview:bgView];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideImage:)];
        [bgView addGestureRecognizer:tap];
        
        [UIView animateWithDuration:0.25 animations:^{
            bgView.alpha = 0.8;
            imageView.frame = CGRectMake(0,([UIScreen mainScreen].bounds.size.height-image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width)/2, [UIScreen mainScreen].bounds.size.width, image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width);
            bgView.alpha = 1;
        }];
    }
    return self;
}

-(void)bannerView:(YJYBannerView *)bannerView selectAtIndex:(NSInteger)index{
    [UIView animateWithDuration:10 animations:^{
        bannerView.frame = [orginImageView convertRect:orginImageView.bounds toView:[UIApplication sharedApplication].keyWindow];
//        bannerView.middleImageView.frame = [orginImageView convertRect:orginImageView.bounds toView:[UIApplication sharedApplication].keyWindow];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        orginImageView.alpha = 1;
        //        bgView.alpha = 0;
    }];
//    [self removeFromSuperview];
}


-(void)savePhotoToAlbum{
    
//    GZRCAlertView *alertView = [[GZRCAlertView alloc]initWithFrame:self.bounds title:@"" message:@"确定要保存图片到相册吗?" alertViewBlock:^(BOOL selectType) {
//        if (selectType) {
//            UIImageWriteToSavedPhotosAlbum(orginImageView.image, self, @selector(image:didFinishedSavingWithError:contextInfo:), (__bridge void *)self);
//        }
//    }];
//    [alertView showInView];
    
    
    
    
}


-(void)image:(UIImage *)image didFinishedSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    NSLog(@"image:%@ , error:%@, contextInfo:%@", image, error, contextInfo);
    if (!error) {
//        [BZYProgressHUD bzy_showMessage:@"图片保存成功" animated:YES];
    }
}

//隐藏图片
-(void)hideImage:(UITapGestureRecognizer *)tap{
    UIView *bgView = tap.view;
    UIImageView *imageView = (UIImageView *)[tap.view viewWithTag:1];
    bgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
    [UIView animateWithDuration:0.25 animations:^{
        imageView.frame = [orginImageView convertRect:orginImageView.bounds toView:[UIApplication sharedApplication].keyWindow];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        orginImageView.alpha = 1;
//        bgView.alpha = 0;
    }];
}


@end
